//
//  SearchGoodsListViewController.m
//  Findif
//
//  Created by Mac on 2018/12/4.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "SearchGoodsListViewController.h"
#import "HomeTableViewCell.h"
#import "GoodsModel.h"
#import "GoodsDetailsViewController.h"

@interface SearchGoodsListViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UITextField * searchTextfiled;
@property (nonatomic, strong)NSArray<GoodsModel *> * goodsListArray;

@end

@implementation SearchGoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ZRWhiteColor;
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    //    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    //    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIButton * backButton1 = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0 , 0 , 65 * SCREENH6, 20 * SCREENH6);
    [backButton1 setTitle:@"取消" forState:UIControlStateNormal];
    backButton1.titleLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    [backButton1 setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    
    [backButton1 addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
}


- (void)setupUI{
    
    UITextField * searchTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 5 * SCREENH6, 260 * SCREENH6, 30 * SCREENH6)];
    
    searchTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    searchTextfiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    searchTextfiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xbebebe);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    searchTextfiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索店铺名/商品名" attributes:attrs];
    searchTextfiled.textColor = [UIColor blackColor];
    searchTextfiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 16 * SCREENH6, 17.5 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = [UIImage imageNamed:@"hg"];
    [rightView addSubview:image];
    searchTextfiled.leftView = rightView;
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.returnKeyType = UIReturnKeySearch;
    searchTextfiled.delegate = self;
    
    self.searchTextfiled = searchTextfiled;
    
    //    [view addSubview:self.searchTextfiled];
    
    self.navigationItem.titleView = self.searchTextfiled;
    
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF8F9FA);
    tableView.rowHeight = 120 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"SearchHomeTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.goodsListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SearchHomeTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.goodsListArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    GoodsModel * model = self.goodsListArray[indexPath.row];
    GoodsDetailsViewController * detailVC = [GoodsDetailsViewController new];
    detailVC.ID = model.id;
    detailVC.infoType = [NSString stringWithFormat:@"%@",model.merchants_goods_type];
    [self.navigationController pushViewController:detailVC animated:YES];

}


//实现UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    [[ZRNetworkTool sharedTool] searchGoodsResultWithSearch_name:textField.text WithBlock:^(BOOL success, id responseData) {
        
        self.goodsListArray = [NSArray yy_modelArrayWithClass:[GoodsModel class] json:responseData[@"data"]];
    }];
    
    return YES;
}


- (void)setGoodsListArray:(NSArray<GoodsModel *> *)goodsListArray{
    
    _goodsListArray = goodsListArray;
    
    [self.tableView reloadData];
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
